package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketRole;
import com.cskaoyan.market.db.domain.MarketRoleExample;
import com.cskaoyan.market.db.mapper.MarketRoleMapper;
import com.cskaoyan.market.service.AdminRoleService;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.MybatisUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘国兴
 * @description 管理员角色
 */
public class AdminRoleServiceImpl implements AdminRoleService {

    @Override
    public String options() throws JsonProcessingException {
        SqlSession session = MybatisUtil.getSession();
        MarketRoleMapper mapper = session.getMapper(MarketRoleMapper.class);
        List<MarketRole> marketRoles = mapper.selectByExampleSelective(new MarketRoleExample(),MarketRole.Column.id,MarketRole.Column.name);
        Map<String, Object> roleMap = null;
        ArrayList<Map> roleList = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            roleMap = new HashMap<>();
            roleMap.put("value", marketRole.getId());
            roleMap.put("label", marketRole.getName());
            roleList.add(roleMap);
        }
        Object okList = ResponseUtil.okList(roleList);
        return JacksonUtil.writeValueAsString(okList);
    }
}
